package com.itheima.demo;

import com.itheima.domain.Student;

import java.lang.reflect.Constructor;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/7/17 15:01
 */
@SuppressWarnings("all")
public class Demo_004 {
    public static void main(String[] args) throws Exception {
        Class clazz = Student.class;
        method(clazz);
        //有参的构造方法!
        Constructor constructor = clazz.getConstructor(String.class, int.class);
        //有参构造方法创建对象;
        Student stu = (Student) constructor.newInstance("robin", 10);
        Student stu1 = (Student) constructor.newInstance("赵四", 44);

        System.out.println(stu);
        System.out.println(stu1);
    }

    private static void method(Class clazz) throws NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
        //获取空参构造方法;
        Constructor constructor = clazz.getConstructor();
        //通过空参构造创建对象;
        Student stu = (Student) constructor.newInstance();

        System.out.println(stu);
    }
}
